--
-- Author: yawei.zhu
-- Date: 2018-12-15 14:42:09
--
-- PvpChallengeLeftPanel
--
local NewBoxTipsView = require("app.newBag.views.NewBoxTipsView")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChallengeLeftNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.btn_doubt:addClickEventListener(function()
        executeFunc(self._onOperate, "showChallengeNotice", self._challengeInfo)
    end)

    self._ui.img_box:setTouchEnabled(true)
    self._ui.img_box:addClickEventListener(function()
        self:_showRareInfo()
    end)

    -- 优化
    display.setNodesMiddle(self._ui.Panel_time, self._ui["Panel_time/img_dot"], self._ui["Panel_time/tlt"])
end

function ClassRef:refresh()
    if not self._data then return end
end

function ClassRef:updateInfo(info)
    self._challengeInfo = info
    self._ui.tlt:setString(info:getFullTitle())
    self._ui.time:setString(info:getDateStr())
    self._ui.box_num:setString(info.lv)

    local levelInfo = GD:getChallengeLevelInfo(info.lv)
    local needScore = tonumber(levelInfo.levelupExp)
    -- info.score 表示的是当前总的经验值
    local curScore = info.score - tonumber(levelInfo.totalExp)
    self._ui["Panel/Panel_bar/num"]:setString(string.format("%d/%d", curScore, needScore))
    self._ui.LoadingBar:setPercent(curScore * 100 / needScore)

    -- 开启时间，默认有两段开启时间
    local szTimes = string.split(GD:getSystemStrPar("challengeGameTime"), "|")
    self._ui.time_1:setString(szTimes[1])
    self._ui.time_2:setString(szTimes[2])

    local function setTimeTip(index)
        local txtTip = self._ui["tips_"..index]
        local txtTime = self._ui["time_"..index]

        local strTime = szTimes[index]
        local startHour, startMin, endHour, endMin = string.match(strTime, "(%d+):(%d+)-(%d+):(%d+)")
        local startTime = TimeUtil.timeToSeconds(strTime)
        local serverDate = app.session:getServerDate()

        local curTime = serverDate.hour * 3600 + serverDate.min * 60 + serverDate.sec
        local startTime = startHour * 3600 + startMin * 60
        local endTime = endHour * 3600 + endMin * 60
        if curTime < startTime then
            txtTip:setString(L("lua_code_text_161"))
            txtTip:setTextColor(cc.c4b(0xAC, 0xA5, 0x9A, 0xFF))
            txtTime:setOpacity(255 * 0.4)
        elseif startTime <= curTime and curTime < endTime then
            txtTip:setString(L("lua_code_text_232"))
            txtTip:setTextColor(cc.c4b(0x9C, 0xFF, 0x00, 0xFF))
            txtTime:setOpacity(255)
        else
            txtTip:setString(L("lua_code_text_361"))
            txtTip:setTextColor(cc.c4b(0xBC, 0x1B, 0x1B, 0xFF))
            txtTime:setOpacity(255 * 0.4)
        end
    end

    setTimeTip(1)
    setTimeTip(2)
end

function ClassRef:_showRareInfo()

    local dataTemp = {
        name = L("seasonlv_name"),
        desc = L("challengeSeasonTips"),
        notice = L("tips_3000412"),
    }
    local rewards = {}
    local level = self._challengeInfo.lv
    if level > 0 then
        local conf = GD:getChallengeLevelInfo(level)
        if conf then
            local dropIds = string.split(conf.seasonReward, ";")
            for _, dropId in ipairs(dropIds) do
                local rets = UD:parseDropIDToBagItem(dropId)
                if rets then
                    for _, itemData in ipairs(rets) do
                        table.insert(rewards, itemData)
                    end
                end
            end
        end
    end
    dataTemp.rewards = rewards

    local rarePanel = NewBoxTipsView.create()
    rarePanel:setData(dataTemp)
    UIHandler:addPopupUi(rarePanel)
    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)

    local node = self._ui.img_box
    local box = node:getBoundingBox()
    local worldPos = node:convertToWorldSpace(cc.p(0, 0))
    local nodeHeight = rarePanel:getContentSize().height

    -- display.adjustNodePos 会把rarePanel默认和box的最上面对齐，这里需要中间对齐，所以就手动设置坐标了
    rarePanel:setPositionX(worldPos.x + box.width)
    rarePanel:setPositionY(worldPos.y + box.height / 2  - nodeHeight / 2)
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
